package com.yugao.fintech.antelope.pay.payment.model.bo;

import com.yugao.fintech.antelope.pay.constant.IfCodeEnum;
import com.yugao.fintech.antelope.pay.model.entity.MchApp;
import com.yugao.fintech.antelope.pay.model.entity.MchInfo;
import com.yugao.fintech.antelope.pay.payment.model.params.IsvsubMchParams;
import com.yugao.fintech.antelope.pay.payment.model.params.NormalMchParams;
import com.yugao.fintech.framework.assistant.utils.exception.Assert;
import lombok.Data;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 商户应用支付参数信息
 * 放置到内存， 避免多次查询操作
 */
@Data
public class MchAppConfigContext {

    /**
     * 商户信息缓存
     */
    private String mchNo;
    private String appId;
    private String mchType;
    private MchInfo mchInfo;
    private MchApp mchApp;

    /**
     * 商户支付配置信息缓存,  <接口代码, 支付参数>
     */
    private Map<String, NormalMchParams> normalMchParamsMap = new ConcurrentHashMap<>();
    private Map<String, IsvsubMchParams> isvsubMchParamsMap = new ConcurrentHashMap<>();
    private Map<IfCodeEnum, IClientWrapper> clientWrapperMap = new ConcurrentHashMap<>();

    /**
     * 放置所属服务商的信息
     */
    private IsvConfigContext isvConfigContext;

    /**
     * 获取普通商户配置信息
     */
    public NormalMchParams getNormalMchParamsByIfCode(IfCodeEnum ifCode) {
        return normalMchParamsMap.get(ifCode.getCode());
    }


    /**
     * 获取特约商户配置信息
     */
    public IsvsubMchParams getIsvsubMchParamsByIfCode(IfCodeEnum ifCode) {
        return isvsubMchParamsMap.get(ifCode.getCode());
    }

    /**
     * 是否为 服务商特约商户
     */
    public boolean isIsvsubMch() {
        return Objects.equals(this.mchType, MchInfo.TYPE_ISVSUB);
    }

    public void addClientWrapper(IfCodeEnum ifCodeEnum, IClientWrapper clientWrapper) {
        Assert.notNull(ifCodeEnum, "ifCodeEnum is null");
        clientWrapperMap.put(ifCodeEnum, clientWrapper);
    }

    public IClientWrapper getClientWrapper(IfCodeEnum ifCodeEnum) {
        return clientWrapperMap.get(ifCodeEnum);
    }
}
